//
//  MainVC.swift
//  swift微博
//
//  Created by CC on 16/2/26.
//  Copyright © 2016年 CC. All rights reserved.
//

import UIKit
                                                //5.遵守协议
class MainVC: UITabBarController,MainTabBarButtonDeleget{

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // tabBar是只读的,使用KVC来赋值
        //        tabBar = UITabBar()
        let mainTabBar = MainTB()
        //5.遵守代理
        mainTabBar.buttonDelegate = self
        setValue(mainTabBar, forKey: "TabBar")

        addChildViewControllers()
        
    }
    
    // MARK: - 实现代理方法,响应按钮点击事件
    // 6.实现代理方法
    func MainTabBarButtonDidClick() {
        print("在 CZMainViewController 响应到了按钮的点击事件")
    }

    func addChildViewControllers() {
        //首页
        let HomeVC = HomeViewController()
        addChildViewController(HomeVC, title: "首页", image: "tabbar_home")
        
        //消息
        let messageVC = MessageViewController()
        addChildViewController(messageVC, title: "消息", image: "tabbar_message_center")
    
        // 占位的tabBar
        addChildViewController(UIViewController())
        
        //我
        let profileVC = ProfileViewController()
        addChildViewController(profileVC, title: "我", image: "tabbar_profile")
        
        //发现
        let DiscoveryVC = DiscoveryViewController()
        addChildViewController(DiscoveryVC, title: "发现", image: "tabbar_discover")
        
        //
        

    }
    
    func addChildViewController(Controller: UIViewController , title : String ,  image : String ) {
        Controller.title = title
        Controller.tabBarItem.image = UIImage(named: image)
        //图片不要渲染设置
        let selectedImageName = image + "_highlighted"
        Controller.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 设置选中文字的颜色
        Controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        // 添加为tabBar的子控制器,并且包装导航控制器
        addChildViewController(UINavigationController(rootViewController : Controller))
    }
    
}
